package com.pic.mgt.haishin;

import com.pic.mgt.executer.UnitDispatchException;
import com.pic.mgt.executer.UnitDispatcher;
import com.pic.mgt.executer.UnitExecuter;
import com.pic.mgt.haishin.unit.AppointedAsgmtOkHaishinMessageUnit;
import com.pic.mgt.haishin.unit.AppointedTargetOkHaishinMessageUnit;
import com.pic.mgt.haishin.unit.GeneralTargetOkHaishinMessageUnit;
import com.pic.mgt.log.IgxLog;
import com.pic.mgt.unit.Unit;

public final class HaishinMessageUnitDispatcher extends UnitDispatcher {
	private UnitExecuter generalHaishinMessageUnitExecuter;
	private UnitExecuter appointedHaishinMessageUnitExecuter;

	@Override
	protected void execute(Unit unit) {
		if (AppointedTargetOkHaishinMessageUnit.class.isInstance(unit) ||
			AppointedAsgmtOkHaishinMessageUnit.class.isInstance(unit)) {
			// 指示配信
			
			// ********************** Log **********************
			if (IgxLog.messageLog.isDebugMode()) {
				IgxLog.messageLog.debug(this, "Dispatching message unit '{}' to appointed haishin message unit executer.",
							unit.getUnitName());
			}
			// ********************** Log **********************
			
			this.appointedHaishinMessageUnitExecuter.execute(unit);
		}
		else if (GeneralTargetOkHaishinMessageUnit.class.isInstance(unit)) {
			// 一般配信
			
			// ********************** Log **********************
			if (IgxLog.messageLog.isDebugMode()) {
				IgxLog.messageLog.debug(this, "Dispatching message unit '{}' to general haishin message unit executer.",
							unit.getUnitName());
			}
			// ********************** Log **********************
			
			this.generalHaishinMessageUnitExecuter.execute(unit);
		}
		else {
			try {
				throw new UnitDispatchException(
						"SUDA-251 Dispatcher can't find match executer for message unit '" +
						unit.getClass().getName() + "'.");
			} catch (UnitDispatchException e) {
				// ********************** Log **********************
				IgxLog.messageLog.error(this, e.getMessage());
				// ********************** Log **********************
			}
		}
 	}
	
	// --- getter and setter ---

	public void setGeneralHaishinMessageUnitExecuter(
			UnitExecuter generalHaishinMessageUnitExecuter) {
		this.generalHaishinMessageUnitExecuter = generalHaishinMessageUnitExecuter;
	}

	public void setAppointedHaishinMessageUnitExecuter(
			UnitExecuter appointedHaishinMessageUnitExecuter) {
		this.appointedHaishinMessageUnitExecuter = appointedHaishinMessageUnitExecuter;
	}
	
}
